import 'dart:math'; // 导入 dart:math 库


void main() {

//https://dart.cn/language/classes/#using-class-members

var p = Point(2, 2);

// Get the value of y.
assert(p.y == 2);

// Invoke distanceTo() on p.
double distance = p.distanceTo(Point(4, 4));

// If p is non-null, set a variable equal to its y value.
var a = p?.y;


//https://dart.cn/language/classes/#using-constructors
// 使用构造函数
  var p1 = Point(2, 2);
  var p2 = Point.fromJson({'x': 1, 'y': 2});

  // 使用常量构造函数
  var p3 = const ImmutablePoint(2, 2);
  var a1 = const ImmutablePoint(1, 1);
  var b1 = const ImmutablePoint(1, 1);

  assert(identical(a1, b1)); // 它们是同一个实例！

  var a2 = const ImmutablePoint(1, 1); // 创建一个常量实例
  var b2 = ImmutablePoint(1, 1); // 不会创建常量实例

  assert(!identical(a2, b2)); // 不是同一个实例！

  // 使用常量上下文
  const pointAndLine = {
    'point': [ImmutablePoint(0, 0)],
    'line': [ImmutablePoint(1, 10), ImmutablePoint(-2, 11)],
  };



var point = Point1();
  point.x = 4; // Use the setter method for x.
  assert(point.x == 4); // Use the getter method for x.
  assert(point.y == null); // Values default to null.


print(greetBob(Person('Kathy')));
  print(greetBob(Impostor()));

assert(Queue.initialCapacity == 16);


var a0 = Point2(2, 2);
  var b = Point2(4, 4);
 
  assert(2.8 < distance && distance < 2.9);
  print(distance);





































}



//https://dart.cn/language/classes/#using-constructors
// 定义一个 Point 类
class Point {
  final double x;
  final double y;

  Point(this.x, this.y);

  // 命名构造函数
  Point.fromJson(Map<String, double> json)
      : x = json['x']!,
        y = json['y']!;

  // 计算到另一个点的距离
  double distanceTo(Point other) {
    return (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y);
  }
}

// 定义一个常量类
class ImmutablePoint {
  final double x;
  final double y;

  const ImmutablePoint(this.x, this.y);
}




//https://dart.cn/language/classes/#instance-variables

class Point1 {
  double? x; // Declare instance variable x, initially null.
  double? y; // Declare y, initially null.
}

double initialX = 1.5;

class Point01 {
  // OK, can access declarations that do not depend on `this`:
  double? x = initialX;

  
  // OK, can access `this` in `late` initializer:
  late double? z = this.x;

  // OK, `this.x` and `this.y` are parameter declarations, not expressions:
  Point01(this.x );
}


class ProfileMark {
  final String name;
  final DateTime start = DateTime.now();

  ProfileMark(this.name);
  ProfileMark.unnamed() : name = '';
}


//https://dart.cn/language/classes/#implicit-interfaces
// A person. The implicit interface contains greet().
class Person {
  // In the interface, but visible only in this library.
  final String _name;

  // Not in the interface, since this is a constructor.
  Person(this._name);

  // In the interface.
  String greet(String who) => 'Hello, $who. I am $_name.';
}

// An implementation of the Person interface.
class Impostor implements Person {
  String get _name => '';

  String greet(String who) => 'Hi $who. Do you know who I am?';
}

String greetBob(Person person) => person.greet('Bob');



//https://dart.cn/language/classes/#static-variables

class Queue {
  static const initialCapacity = 16;
  // ···
}






//https://dart.cn/language/classes/#static-methods


class Point2 {
  double x, y;
  Point2(this.x, this.y);

  static double distanceBetween(Point a, Point b) {
    var dx = a.x - b.x;
    var dy = a.y - b.y;
    return sqrt(dx * dx + dy * dy);
  }
}

